<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>积分兑换</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/style_xc.css" />
    <link rel="stylesheet" type="text/css" href="../css/iconfont.css"/>
</head>
<body>
  <div class="myui-jifen-box">
    <div class="myui-jifen-1">
      <h1 id="sign"></h1>当前金币
    </div>
    <div class="myui-jifen-bt">
      <div class="my-jien-btn aui-btn aui-btn-info aui-btn-block aui-btn-sm" tapmode onclick="openWin('share_win')">获取金币</div>
    </div>
        <div class="myui-jifen-bt">

    <div id="bili" class="myui-jifen-2">

    </div>
    <div class="aui-bar aui-bar-btn" style="width:80%;" type="count" id="demo">
        <div class="aui-bar-btn-item aui-font-size-20">
            <i class="aui-iconfont aui-icon-minus"></i>
        </div>
        <div class="aui-bar-btn-item">
            <input type="number" class="aui-input aui-text-center" id="count" value="1">
        </div>
        <div class="aui-bar-btn-item aui-font-size-20">
            <i class="aui-iconfont aui-icon-plus"></i>
        </div>
    </div>
    <div class="myui-jifen-bt">
      <div class="my-jien-btn aui-btn aui-btn-info aui-btn-block aui-btn-sm" tapmode onclick="fasong()">兑换VIP(天)</div>
    </div><br/>
     <div class="aui-btn aui-btn-danger" tapmode onclick="closeWin()">兑换现金</div>
  </div>
<div id="neir">

	</div>


</body>
<script type="text/javascript" src="../script/api.js" ></script>
<script type="text/javascript" src="../script/auitab.js" ></script>
<script type="text/javascript">
  var api_url = localStorage.getItem('api_url');

  apiready = function(){
    api.parseTapmode();
    api.showProgress({
        title: '正在刷新',
        text: '请稍等...',
        modal: false
    });
    api.ajax({
        url: api_url+'login/login/signw.html',
        method: 'get',
        data: {
            values: {
                uid: localStorage.getItem('user_id')
            }
        }
    },function(ret, err){
        if (ret) {
            var sign = $api.byId('sign');
            sign.innerHTML = ret.msg['jinbi'];
						var daybl= ret.msg['daybili'];
						var xjbl= ret.msg['xjbili'];
						var bili = $api.byId('bili');
            localStorage.setItem('user_jinb',ret.msg['jinbi']);
						localStorage.setItem('user_jbdaybl',daybl);
						localStorage.setItem('user_jbxjbl',xjbl);
						bili.innerHTML = daybl+'金币可以兑换1天会员资格！';
					//	bili2.innerHTML = xjbl+'金币可以兑换1元现金!';
						api.hideProgress();
        } else {
          api.hideProgress();
            alert('网络错误11');
        }
    });

    /* api.ajax({
        url: 'http://v26.xin-dianying.com/login/login/share.html',
        method: 'get',
        data: {
            values: {
                uid: localStorage.getItem('user_id')
            }
        }
    },function(ret, err){
        if (ret) {
          var bili = $api.byId('bili');

          bili.innerHTML = '100金币可以兑换1天会员资格！';
					bili2.innerHTML = '500金币可以兑换1元现金!';
          api.hideProgress();
        } else {
          api.hideProgress();
          alert('获取失败');

        }
    }); */
  }
  function closeWin(){
      api.closeWin({
      });
  }
function fasong(){
  var daybilv =localStorage.getItem('user_jbdaybl');
  var bianl1 = document.querySelector('#sign');
  var bianl2 = document.querySelector('#count').value;
  var bianl3 = Number(bianl1.innerText)-(Number(bianl2)*daybilv);//兑换金币/积分比率!!!!
  var bianyu = Number(bianl3);
  if (bianyu<=-1){
      alert('金币不足，无法兑换');
   return;
  }
  var shuji=Number(bianl2);
   sign.innerHTML = bianyu;
   var userid = localStorage.getItem('user_id');
   var student = new Object();
   student.id = userid;
   student.age = shuji;
   var json = JSON.stringify(student);
   api.ajax({
     url: api_url+'appx/cesaj.php',
     method: 'post',
     data: {
         values: {
             json: json
           }
       }
     }, function(ret, err) {
       if (ret.code==1) {
           localStorage.setItem('user_jinb',bianyu);
           alert('恭喜您兑换' + ret.msg + '天会员成功！'); 
       } else {
           alert(ret.msg);
           //api.alert({ msg: JSON.stringify(err) });
       }

     });
}
 function fasong1(){
      var ajax = new XMLHttpRequest();
      ajax.open('post',api_url+'appx/cesaj.php');
      ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	       var daybilv =localStorage.getItem('user_jbdaybl');
			 var bianl1 = document.querySelector('#sign');
			 var bianl2 = document.querySelector('#count').value;
			 var bianl3 = Number(bianl1.innerText)-(Number(bianl2)*daybilv);//兑换金币/积分比率!!!!
			 var bianyu = Number(bianl3);
			 if (bianyu<=-1){
			     alert('金币不足，无法兑换');
			 	return;
		   }
			 	 var shuji=Number(bianl2);
			 	  sign.innerHTML = bianyu;
					var userid = localStorage.getItem('user_id');
			 		var student = new Object();
			 		student.id = userid;
			 		student.age = shuji;
			 		var json = JSON.stringify(student);
			 		ajax.send('json='+json);
			 	ajax.onreadystatechange = function(){
			 		if (ajax.readyState ==4&&ajax.status==200) {
			 				console.log(ajax.responseText);
							localStorage.setItem('user_jinb',bianyu);
							alert('恭喜您兑换' + ajax.responseText + '天会员成功！');
			 		}
			 	};
    }


  //重新获取到期时间
//   function getTime(){
//     api.ajax({
//         url: 'http://hjhkjk.xyz/login/login/veifys.html',
//         method: 'get',
//         data: {
//             values: {
//                 username: localStorage.getItem('user_name'),
//                 passwd: localStorage.getItem('password'),
//                 imei:api.deviceId
//             }
//         }
//     },function(ret, err){
//         if (ret) {
//           if (ret.code == 0) {
//             api.hideProgress();
//             alert('不明错误 请重新登录');
//           }else if (ret.code == 1) {
//               localStorage.setItem('user_time',ret.msg['time']);
//               api.sendEvent({
//                 name: 'newLoingdata',
//                 extra: {
//                 }
//               });
//           }
//         } else {
//           api.hideProgress();
//           alert('网络错误！33');
//         }
//     });
//   }


  var bar = document.querySelectorAll(".aui-bar-btn");
    if(bar){
        for(var i=0; i<bar.length;i++){
            var d = bar[i];
            var tab = new auiTab({
                element:bar[i],
                repeatClick:true
            },function(ret){
                //alert( JSON.stringify( ret ) );
                if(ret.dom.parentNode.getAttribute("type") && ret.dom.parentNode.getAttribute("type")=="count"){
                    var count = parseInt(document.getElementById("count").value);
                    if(ret.index==2)return;
                    if(ret.index==1){
                      if (document.getElementById("count").value<=1) {
                        return;
                      }
                        document.getElementById("count").value = count-1;
                    }
                    if(ret.index==3){
                        document.getElementById("count").value = count+1;
                    }
                }
            });

        }
    }


   function openWin(name){
        var delay = 0;
        if(api.systemType != 'ios'){
            delay = 300;
        }
        api.openWin({
            name: ''+name+'',
            url: ''+name+'.html',
            bounces:false,
            delay: delay,
            slidBackEnabled:true,
            vScrollBarEnabled:false
        });
    }
</script>
</html>
